//
//  PersonalDataViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/10/20.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "PersonalDataViewController.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
#import "ReceiptAddressViewController.h"
#import "PopViewController.h"
@interface PersonalDataViewController ()<UITableViewDelegate,UITableViewDataSource,UIPopoverPresentationControllerDelegate>
{
    PopViewController *_popVC;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSourceArr;

@property (nonatomic,assign)BOOL a;
@property (nonatomic,assign)BOOL b;

@end

@implementation PersonalDataViewController
- (NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithObjects:@"头像",@"会员名",@"昵称",@"性别",@"我的二维码名片",@"我的收获地址", nil];
    }
    return _dataSourceArr;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //one
    _popVC = [[PopViewController alloc] init];
    _popVC.view.frame = CGRectMake(200, 70, 80, 100);
    self.a = YES;
    self.b = YES;
    
    [self.view addSubview:self.tableView];
    //导航栏添加控件
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"← 个人资料" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
        self.navigationItem.leftBarButtonItem = backItem;

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"message"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(popViewAction)];
    self.navigationItem.rightBarButtonItem = rightItem;

}
- (void)backAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark new
- (void)popViewAction1{
    if (self.a == YES) {
        [UIView animateWithDuration:0.1f animations:^{
            self.tableView.userInteractionEnabled = NO;
            [self addChildViewController:_popVC];
            [self.view addSubview:_popVC.view];
        } completion:^(BOOL finished) {
            self.a = NO;
        }];
        
    }
    
    if (self.a == NO) {
        [UIView animateWithDuration:0.1f animations:^{
            self.tableView.userInteractionEnabled = YES;
            [self.view  bringSubviewToFront:self.tableView ];
        } completion:^(BOOL finished) {
            self.a = YES;
        }];
        
    }
    
    
}


//点击导航栏右侧按钮弹出悬浮框
-(void)popViewAction{

    _popVC = [[PopViewController alloc] init];

    _popVC.modalPresentationStyle = UIModalPresentationPopover;

    //设置依附的按钮
    _popVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;

    //可以指示小箭头颜色
    _popVC.popoverPresentationController.backgroundColor = [UIColor whiteColor];

    //content尺寸
    _popVC.preferredContentSize = CGSizeMake(400, 400);

    //pop方向
    _popVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    //delegate
    _popVC.popoverPresentationController.delegate = self;
    
    _popVC.viewController = self;
    [self presentViewController:_popVC animated:YES completion:nil];


}
//代理方法 ,点击即可dismiss掉每次init产生的PopViewController
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    return UIModalPresentationNone;
}
#pragma mark ----- UITableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
       return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLabel.text = self.dataSourceArr [indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;


}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.row == 0) {
            return 100;
        }else{
            return 50;

    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.row == 5) {
            ReceiptAddressViewController *addressVC = [[ReceiptAddressViewController alloc]init];
            [self.navigationController pushViewController:addressVC animated:YES];
        }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
